<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/login.css">
</head>
<body>
    <div class="nav">
        网络五子棋对战游戏
    </div>
    <div class="login-container">
        <!-- 登录界面的对话框 -->
        <div class="login-dialog">
            <!-- 提示信息 -->
            <h3>注册</h3>
            <!-- 这个表示一行 -->
            <div class="row">
                <span>用户名</span>
                <input type="text" id="user_name" name="username">
            </div>
            <!-- 这是另一行 -->
            <div class="row">
                <span>密码</span>
                <input type="password" id="password" name="password">
            </div>
            <!-- 提交按钮 -->
            <div class="row">
                <button id="submit" onclick="reg()">提交</button>
            </div>
        </div>
    </div> 

    <script src="js/jquery.min.js"></script>
    <script>
        function reg()
        {
          //获取2个输入框的数据组织成一个Json块
          var reg_info = {
            username : document.getElementById("user_name").value, 
            password : document.getElementById("password").value 
          };
          //2.通过ajax向后台发送用户注册请求 
          $.ajax({
            url : "/reg" , 
            type : "post" , 
            data : JSON.stringify(reg_info), 
            success : function(res)
            {
                //跳转登陆页面
                alert(res.reason);
                window.location.assign("/login.html");
            },
            error : function(xhr)
            {
                 //清空输入框内容并返回原因
                 username : document.getElementById("user_name").value= ""; 
                 password : document.getElementById("password").value = ""; 
                 alert(xhr.reason);
            }
          })
        } 
    </script>
</body>
</html>
